package com.example.statistics.common

import android.os.Bundle
import android.view.View
import com.example.statistics.net.DataResult
import com.example.statistics.net.callback.DataHttpCallback
import com.jhj.httplibrary.HttpCall
import com.jhj.httplibrary.model.HttpParams
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import kotlinx.android.synthetic.main.layout_empty_view.*
import java.lang.reflect.Type

abstract class BaseLoadingListActivity<T> : BaseListActivity<T>() {

    abstract val url: String

    //请求参数
    open val httpParams = mutableListOf<Pair<String, String>>()

    //输入框变化就开始搜索
    val inputSearchParams: HttpParams = HttpParams()

    open val inputSearchKey = "search"
    open val inputSearchFunc: (T, String) -> Boolean = { _, _ -> true }

    var isRefresh = true
    var pageSize = 20
    var pageNo = 1


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include_empty_view.visibility = View.VISIBLE
        adapterLocal.setOnLoadMoreListener {
            isRefresh = false
            httpRequest()
        }
    }

    override fun refresh() {
        pageNo = 1
        isRefresh = true
        httpRequest()
    }

    override fun inputSearch(search: String?) {
        pageNo = 1
        isRefresh = true
        inputSearchParams.clear()
        if (!search.isNullOrBlank()) //删除完成后自动搜索
            inputSearchParams.put(inputSearchKey, search)
        httpRequest()
    }


    private fun httpRequest() {
        val params = HttpParams()
        httpParams.forEach {
            params.put(it.first, it.second)
        }
        params.put("pageIndex", pageNo)
        params.put("pageSize", pageSize)
        HttpCall.get(url)
            .addParams(params)
            .addParams(inputSearchParams)
            .addParams(filterParams)
            .enqueue(object : DataHttpCallback<List<T>>(this) {

                var isSuccess = false

                override val clazzType: Type
                    get() = type(DataResult::class.java, type(List::class.java, genericType()))

                override fun onSuccess(data: List<T>?, resultType: ResultType) {
                    isSuccess = true
                    if (isRefresh) {
                        dataList = data.orEmpty().toMutableList()
                    } else {
                        adapterLocal.addDataList(data.orEmpty())
                    }
                    if (data?.size == pageSize) {
                        pageNo++
                    } else {
                        adapterLocal.loadMoreEnd()
                    }
                }

                override fun onFailure(msg: String) {
                    super.onFailure(msg)
                    isSuccess = false
                    adapterLocal.loadMoreFail()
                }

                override fun onFinish() {
                    super.onFinish()
                    if (!isSuccess) {
                        include_empty_view.visibility = View.VISIBLE
                        tv_refresh_reason.text = "查询失败"
                    }
                }
            })
    }
}